package com.example.administrator.yuejian2.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.app.Fragment;
import android.widget.Toast;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.alibaba.android.vlayout.layout.LinearLayoutHelper;
import com.alibaba.android.vlayout.layout.StaggeredGridLayoutHelper;
import com.alibaba.android.vlayout.layout.StickyLayoutHelper;
import com.example.administrator.yuejian2.R;
import com.example.administrator.yuejian2.RetrofitListener.RetrofitListener;
import com.example.administrator.yuejian2.adapter.ActivityAdapter;
import com.example.administrator.yuejian2.adapter.ClubAdapter;
import com.example.administrator.yuejian2.adapter.ItemAdapter;
import com.example.administrator.yuejian2.adapter.VLayoutClubAdapter;
import com.example.administrator.yuejian2.bean.ActivityBean;
import com.example.administrator.yuejian2.bean.ClubBean;
import com.example.administrator.yuejian2.model.ActivityModel;
import com.example.administrator.yuejian2.model.ClubModel;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class ActivityFragment extends Fragment {

    private RecyclerView recyclerView;
    private ActivityAdapter activityAdapter;
    private RecyclerView.LayoutManager layoutManager;
    public RetrofitListener<List<ActivityBean>> listener;
    public ActivityFragment() {
        // Required empty public constructor
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        getactivity();
       // club();
    }
    private void initView(View v)
    {
        recyclerView=v.findViewById(R.id.activity_recyclerview);
        VirtualLayoutManager virtualLayoutManager = new VirtualLayoutManager(getContext());
        recyclerView.setLayoutManager(virtualLayoutManager);
        //设置view缓存个数
        final RecyclerView.RecycledViewPool viewPool = new RecyclerView.RecycledViewPool();
        recyclerView.setRecycledViewPool(viewPool);
        viewPool.setMaxRecycledViews(0, 20);
        //RecylerView创建适配器，初始化DelegateAdapter
        DelegateAdapter delegateAdapter = new DelegateAdapter(virtualLayoutManager, false);
        recyclerView.setAdapter(delegateAdapter);
        List<DelegateAdapter.Adapter> adapters=new LinkedList<>();
        delegateAdapter.addAdapters(adapters);
        activityAdapter=new ActivityAdapter(getContext(),new LinearLayoutHelper(5));
        delegateAdapter.addAdapter(activityAdapter);

    /*    layoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutManager);
        activityAdapter = new ActivityAdapter(getActivity());
        recyclerView.setAdapter(activityAdapter);*/

    }


    private void getactivity()
    {
        ActivityModel activityModel=new ActivityModel();
        activityModel.activityList(listener);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        listener=new RetrofitListener<List<ActivityBean>>() {
            @Override
            public void onSuccess(List<ActivityBean> list, int flag) {
                activityAdapter.activityData(list);
            }

            @Override
            public void onFail() {
                Toast.makeText(getActivity(),"获取失败",Toast.LENGTH_SHORT).show();
            }
        };


        View v= inflater.inflate(R.layout.fragment_activity, container, false);
        return v;
    }

}
